Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
command-line-commands
Advanced tools
A lightweight module to help build a git-like command interface for your app.
Its job is to extract the command (the first argument, unless it's an option), check it's valid and either return it or throw. From there, you can parse the remaining args using your preferred option parser (e.g. command-line-args, minimist etc.).
Create a list of valid commands (null
represents "no command"). Supply it to commandLineCommands()
, receiving back an object with two properties: command
(the supplied command) and argv
(the remainder of the command line args):
const commandLineCommands = require('command-line-commands')
const validCommands = [ null, 'clean', 'update', 'install' ]
const { command, argv } = commandLineCommands(validCommands)
/* print the command and remaining command-line args */
console.log('command: %s', command)
console.log('argv: %s', JSON.stringify(argv))
We'll assume the above script is installed as example
. Since the validCommands
list includes null
, running it without a command is valid:
$ example
command: null
argv: []
Running example
with no command and one option:
$ example --verbose
command: null
argv: ["--verbose"]
Running example
with both a command and an option:
$ example install --save something
command: install
argv: ["--save","something"]
Running example
without a valid command will cause commandLineCommands()
to throw.
From here, you can make a decision how to proceed based on the command
and argv
received. For example, if no command (null
) was passed, you could parse the remaining argv
for general options (in this case using command-line-args):
if (command === null) {
const commandLineArgs = require('command-line-args')
const optionDefinitions = [
{ name: 'version', type: Boolean }
]
// pass in the `argv` returned by `commandLineCommands()`
const options = commandLineArgs(optionDefinitions, { argv })
if (options.version) {
console.log('version 1.0.1')
}
}
The same example, using minimist:
if (command === null) {
const minimist = require('minimist')
// pass in the `argv` returned by `commandLineCommands()``
const options = minimist(argv)
if (options.version) {
console.log('version 1.0.1')
}
}
Both examples use command-line-args for option-parsing.
Usage guides can be generated by command-line-usage. Here is a simple example (code):
Example
const commandLineCommands = require('command-line-commands')
Object
⏏Parses the argv
value supplied (or process.argv
by default), extracting and returning the command
and remainder of argv
. The command will be the first value in the argv
array unless it is an option (e.g. --help
).
Kind: Exported function
Throws:
INVALID_COMMAND
- user supplied a command not specified in commands
.Param | Type | Description |
---|---|---|
commands | string | Array.<string> | One or more command strings, one of which the user must supply. Include null to represent "no command" (effectively making a command optional). |
[argv] | Array.<string> | An argv array, defaults to the global process.argv if not supplied. |
© 2015-17 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.
FAQs
Add a git-like command interface to your app.
The npm package command-line-commands receives a total of 5,551 weekly downloads. As such, command-line-commands popularity was classified as popular.
We found that command-line-commands demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.